package com.yami.trading.admin.task;


import com.alibaba.fastjson.JSONObject;
import com.yami.trading.api.websocket.WebSocketServer;
import com.yami.trading.api.websocket.WebSocketSession;
import com.yami.trading.bean.data.domain.Depth;
import com.yami.trading.bean.data.domain.DepthEntry;
import com.yami.trading.bean.data.domain.Realtime;
import com.yami.trading.bean.data.domain.TradeEntry;
import com.yami.trading.bean.item.domain.Item;
import com.yami.trading.common.util.MarketOpenChecker;
import com.yami.trading.common.util.StringUtils;
import com.yami.trading.common.web.ResultObject;
import com.yami.trading.huobi.data.DataCache;
import com.yami.trading.huobi.data.internal.DepthTimeObject;
import com.yami.trading.huobi.data.internal.TradeTimeObject;
import com.yami.trading.huobi.hobi.internal.XueQiuDataServiceImpl;
import com.yami.trading.service.item.ItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

@Component
@Lazy(value = false)
@Slf4j
public class AStockTradeListTask {
    @Autowired
    private DepthPushJob depthPushJob;
    @Autowired
    private ItemService itemService;
    @Autowired
    private XueQiuDataServiceImpl xueQiuDataService;

    private volatile  boolean isAStockInit = false;
    private volatile  boolean isUsStockInit = false;


   // @Scheduled(cron = "*/5 * * * * ?")
    public void sendTask() throws InterruptedException {
        if(!isAStockInit){
            fetchAStock();
            isAStockInit = true;
        }
        if(MarketOpenChecker.isMarketOpenByItemCloseType(Item.A_STOCKS)){
            fetchAStock();
        }

        if(!isUsStockInit){
            fetchUs();

            isUsStockInit = true;
        }
        if(MarketOpenChecker.isMarketOpenByItemCloseType(Item.US_STOCKS)){
            fetchUs();

        }



    }

    private void fetchAStock() {
        String symbols = itemService.list().stream().filter(t->Item.A_STOCKS.equalsIgnoreCase(t.getOpenCloseType())).map(Item::getSymbol).collect(Collectors.joining(","));
        xueQiuDataService.tradeList(symbols, false);
    }

    private void fetchUs() {
        String usSymbols = itemService.findByType(Item.US_STOCKS).stream().map(Item::getSymbol).collect(Collectors.joining(","));
        xueQiuDataService.tradeList(usSymbols, true);
    }


}
